package com.yuejian_06.yuejian_06.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.fragment.ClubFragment;
import com.yuejian_06.yuejian_06.fragment.CourseFragment;
import com.yuejian_06.yuejian_06.fragment.HomeFragment;
import com.yuejian_06.yuejian_06.fragment.MeFragment;
import com.yuejian_06.yuejian_06.fragment.PicFragment;

/**
 * Created by __ZL__ on 2017/9/26.
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {
    private LinearLayout TabHome;
    private LinearLayout TabCourse;
    private LinearLayout TabClub;
    private LinearLayout TabPic;
    private LinearLayout TabMe;

    private ImageView ImgHome;
    private ImageView ImgCourse;
    private ImageView ImgClub;
    private ImageView ImgPic;
    private ImageView ImgMe;

    private Fragment fragment_home;
    private Fragment fragment_course;
    private Fragment fragment_club;
    private Fragment fragment_pic;
    private Fragment fragment_me;

    private ImageButton addpic;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();//初始化事件
        initEvent();//监听
        setSelect(0);//选择了一个默认的fragment
    }
    private void initEvent()
    {
        TabHome.setOnClickListener(this);
        TabCourse.setOnClickListener(this);
        TabClub.setOnClickListener(this);
        TabPic.setOnClickListener(this);
        TabMe.setOnClickListener(this);

        addpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent();
                intent.setClass(MainActivity.this,PostPicActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }
    private void initView()
    {
        TabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        TabCourse = (LinearLayout) findViewById(R.id.id_tab_course);
        TabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        TabPic = (LinearLayout) findViewById(R.id.id_tab_pic);
        TabMe = (LinearLayout) findViewById(R.id.id_tab_me);

        ImgHome = (ImageView) findViewById(R.id.id_tab_home_img);
        ImgCourse = (ImageView) findViewById(R.id.id_tab_course_img);
        ImgClub = (ImageView) findViewById(R.id.id_tab_club_img);
        ImgPic = (ImageView) findViewById(R.id.id_tab_pic_img);
        ImgMe = (ImageView) findViewById(R.id.id_tab_me_img);

        addpic = (ImageButton) findViewById(R.id.addpic);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (fragment_home == null)
                {
                    fragment_home = new HomeFragment();
                    transaction.add(R.id.id_content, fragment_home);
                } else
                {
                    transaction.show(fragment_home);
                }
                ImgHome.setImageResource(R.drawable.tab_home_p);
                break;

            case 1:
                if (fragment_course == null)
                {
                    fragment_course = new CourseFragment();
                    transaction.add(R.id.id_content, fragment_course);
                } else
                {
                    transaction.show(fragment_course);
                }
                ImgCourse.setImageResource(R.drawable.tab_course_p);
                break;


            case 2:
                if (fragment_club == null)
                {
                    fragment_club = new ClubFragment();
                    transaction.add(R.id.id_content, fragment_club);
                } else
                {
                    transaction.show(fragment_club);

                }
                ImgClub.setImageResource(R.drawable.tab_club_p);
                break;


            case 3:
                if (fragment_pic == null)
                {
                    fragment_pic = new PicFragment();
                    transaction.add(R.id.id_content, fragment_pic);
                } else
                {
                    transaction.show(fragment_pic);
                }
                ImgPic.setImageResource(R.drawable.tab_pic_p);
                break;

            case 4:
                if (fragment_me == null)
                {
                    fragment_me = new MeFragment();
                    transaction.add(R.id.id_content, fragment_me);
                } else
                {
                    transaction.show(fragment_me);
                }
                ImgMe.setImageResource(R.drawable.tab_me_p);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (fragment_course != null)
        {
            transaction.hide(fragment_course);
        }
        if (fragment_club != null)
        {
            transaction.hide(fragment_club);
        }
        if (fragment_home != null)
        {
            transaction.hide(fragment_home);
        }
        if (fragment_pic != null)
        {
            transaction.hide(fragment_pic);
        }
        if (fragment_me != null)
        {
            transaction.hide(fragment_me);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_course:
                setSelect(1);
                break;
            case R.id.id_tab_club:
                setSelect(2);
                break;
            case R.id.id_tab_pic:
                setSelect(3);
                break;
            case R.id.id_tab_me:
                setSelect(4);
                break;

            default:
                break;
        }
    }
    private void resetImgs()
    {
        ImgCourse.setImageResource(R.drawable.tab_course_n);
        ImgClub.setImageResource(R.drawable.tab_club_n);
        ImgHome.setImageResource(R.drawable.tab_home_n);
        ImgPic.setImageResource(R.drawable.tab_pic_n);
        ImgMe.setImageResource(R.drawable.tab_me_n);
    }
}
